package leetcode.code0914;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public boolean hasGroupsSizeX(int[] deck) {
		int[] buckets = new int[10000];
		for (int num : deck) {
			buckets[num]++;
		}
		Integer gcd = null;
		for (int cur : buckets) {
			if (cur != 0 && gcd != null) {
				gcd = this.gcd(gcd, cur);
			} else if (cur != 0 && gcd == null) {
				gcd = cur;
			}
		}
		return gcd == null ? false : gcd >= 2;
	}

	private int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}

	@Override
	public void debug1() {
		HelpDebug.compare(true, this.hasGroupsSizeX(HelpDebug.str2array3("[1,1,2,2,2,2]")));
		HelpDebug.compare(true, this.hasGroupsSizeX(HelpDebug.str2array3("[1,2,3,4,4,3,2,1]")));
		HelpDebug.compare(false, this.hasGroupsSizeX(HelpDebug.str2array3("[1,1,1,2,2,2,3,3]")));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
